package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.common.Result;
import com.tjetc.domain.Record;
import com.tjetc.service.RecordService;
import com.tjetc.service.impl.RecordServiceImpl;
import com.tjetc.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/record/*")
@MultipartConfig
public class RecordServlet extends HttpServlet {
    private RecordService recordService = new RecordServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Integer did = Integer.valueOf(request.getParameter("did"));
        Integer isLive = Integer.valueOf(request.getParameter("isLive"));
        String description = request.getParameter("description");
        String result = request.getParameter("result");
        Record record = new Record();
        record.setUid(uid);
        record.setDid(did);
        record.setIsLive(isLive);
        record.setDescription(description);
        record.setResult(result);
        //返回的是最新添加进去的id
        int rid = recordService.add(record);
        new ObjectMapper().writeValue(response.getOutputStream(),rid);
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer did = Integer.valueOf(request.getParameter("did"));
        String curPage = request.getParameter("curPage");
        String ps = request.getParameter("pageSize");
        //接受请求参数
        //空处理:赋值给当前页码
        int currentPage = curPage == null ? 1:Integer.valueOf(curPage);
        //每页显示的条数
        int pageSize = ps == null ? 5 : Integer.parseInt(ps);
        //总记录数
        int count = recordService.getCount(did);
        PageInfo<Record> pageInfo = new PageInfo<>(currentPage, count, pageSize);
        List<Record> list = recordService.list(pageInfo,did);
        pageInfo.setList(list);
        new ObjectMapper().writeValue(response.getOutputStream(),pageInfo);
    }

    protected void out(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer rid = Integer.valueOf(request.getParameter("rid"));
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        boolean b = recordService.outHs(rid, uid);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("出院成功"));
        }else{
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("用户余额不足，请提醒充值后出院"));
        }
    }

}
